package com.boss.salary.service.impl;

import com.boss.common.entity.PageDTO;
import com.boss.framework.dto.PaginationDTO;
import com.boss.salary.constant.SystemDictConstant;
import com.boss.salary.dao.ISalaryBaseRelevanceDao;
import com.boss.salary.entity.SalaryBaseRelevanceDTO;
import com.boss.salary.service.ISalaryBaseRelevanceService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.Arrays;
import java.util.List;
@Service
public class SalaryBaseRelevanceServiceImpl implements ISalaryBaseRelevanceService {
    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(SalaryBaseRelevanceServiceImpl.class);

    @Autowired
    private ISalaryBaseRelevanceDao salaryBaseRelevanceDao;

    @Override
    public PageDTO<SalaryBaseRelevanceDTO> selectSalaryBaseRelevance(PaginationDTO paginationDTO, String province) {
        List<SalaryBaseRelevanceDTO>  list=salaryBaseRelevanceDao.selectSalaryBaseRelevance(paginationDTO,province);
        for(SalaryBaseRelevanceDTO sr:list){
            if (!StringUtils.isEmpty(sr.getCitySalaryCodeList())) {
                sr.setCitySalaryCodeArray(Arrays.asList(sr.getCitySalaryCodeList().split(",")) );
            }
            if (!StringUtils.isEmpty(sr.getCitySalaryNameList())) {
                sr.setCitySalaryNameArray(Arrays.asList(sr.getCitySalaryNameList().split(",")));
            }
        }
        int total = salaryBaseRelevanceDao.selectSalaryBaseRelevanceTotal(province);
        PageDTO<SalaryBaseRelevanceDTO> pageDTO = new PageDTO<>();
        pageDTO.setRows(list);
        pageDTO.setTotal(total);
        return pageDTO;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void batchReplaceSalaryBaseRelevance(List<SalaryBaseRelevanceDTO> srList, UserDTO userDto) {

        for(SalaryBaseRelevanceDTO sr:srList){
            sr.setMofDivCode(userDto.getProvince());
            if(StringUtils.isEmpty(sr.getIsDeleted())){
                sr.setIsDeleted(SystemDictConstant.NOT_DELETED_STR);
            }
            if (!StringUtils.isEmpty(sr.getCitySalaryCodeArray())) {
                sr.setCitySalaryCodeList(sr.getCitySalaryCodeArray().toString().replaceAll("\\[", "").replaceAll("]", "").replaceAll(" ", ""));
            }
            if (!StringUtils.isEmpty(sr.getCitySalaryNameArray())) {
                sr.setCitySalaryNameList(sr.getCitySalaryNameArray().toString().replaceAll("\\[", "").replaceAll("]", "").replaceAll(" ", ""));
            }
        }
        logger.info("维护市县与省本级关联关系！");
        salaryBaseRelevanceDao.mergeIntoSalaryBaseRelevance(srList);
    }
}
